/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.finder;

/**
 *
 * @author seccia
 */
public class CapabilityXPathSearch {

    //private static final Logger LOGGER = Logger.getLogger(CapabilityXPathSearch.class.getName());

    /*public String capabilityIDQuery = "/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRightsCapability[1]/@AccessRightsCapabilityID";

    public String capabilityIssueDateQuery = "/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRightsCapability[1]/@IssueInstant";

    public String firstSubjectIDQuery = "/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRightsCapability[1]/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":Subject/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":SubjectID";
    
    public String firstResourceIDQuery = "/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRightsCapability[1]/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":ResourceID";
    
    public String permittedActionQuery = "/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRightsCapability[1]/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRights/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":AccessRight/"
            + CapabilityNamespaces.ACCESS_CAPABILITY_PREFIX + ":PermittedAction";

    private File capabilitiesDir;

    public CapabilityXPathSearch(File capabilitiesDir) {
        if (!capabilitiesDir.isDirectory()) {
            throw new IllegalArgumentException("No Capabilities Directory specified.");
        }
        this.capabilitiesDir = capabilitiesDir;
    }*/

    /*public ArrayList<CapabilitySearchReturn> doSearchCapability(String userSubject, String userResourceID, String userPermittedAction) {

        ArrayList<CapabilitySearchReturn> result = new ArrayList<CapabilitySearchReturn>();

        // the capability namespace context
        
        // Instantiate an XPathFactory.
        XPathFactory factory = XPathFactory.newInstance();

        // Use the XPathFactory to create a new XPath object
        XPath xPath = factory.newXPath();
        //xPath.setNamespaceContext(namespaceContext);

        for (String fileName : capabilitiesDir.list(new CapabilityFileNameFilter())) {

            File capabilityFile = new File(capabilitiesDir, fileName);

            LOGGER.log(Level.INFO, "Searching in file: {0}", capabilityFile.getAbsolutePath());

            InputSource inputSource = new InputSource(capabilityFile.getAbsolutePath());

            try {

                XPathExpression resourceIDXpe = xPath.compile(firstResourceIDQuery);
                String resourceIDresult = resourceIDXpe.evaluate(inputSource);
                resourceIDresult = resourceIDresult.trim().toLowerCase();
                String myResourceID = userResourceID.trim().toLowerCase();

                if (resourceIDresult.equals(myResourceID)) {

                    LOGGER.log(Level.INFO, "Successfully found User Resource ID: {0}", userResourceID);

                    XPathExpression permittedActionsXpe = xPath.compile(permittedActionQuery);
                    NodeList permittedActionsNodeList = (NodeList) permittedActionsXpe.evaluate(
                            inputSource, XPathConstants.NODESET);

                    for (int i = 0; i < permittedActionsNodeList.getLength(); i++) {

                        String permittedActionResult = permittedActionsNodeList.item(i).getTextContent();
                        permittedActionResult = permittedActionResult.trim().toLowerCase();
                        String myPermittedAction = userPermittedAction.trim().toLowerCase();

                        if (permittedActionResult.equals(myPermittedAction)) {

                            LOGGER.log(Level.INFO, "Successfully found User Permitted Action: {0}", userPermittedAction);

                            XPathExpression issueDateExpression = xPath.compile(capabilityIssueDateQuery);
                            String issueDateResult = issueDateExpression.evaluate(inputSource);

                            LOGGER.log(Level.INFO, "Capability Issue Date: {0}", issueDateResult);

                            XPathExpression capabilityIDExpression = xPath.compile(capabilityIDQuery);
                            String capabilityIDResult = capabilityIDExpression.evaluate(inputSource);

                            LOGGER.log(Level.INFO, "Capability ID: {0}", capabilityIDResult);

                            CapabilitySearchReturn csr = new CapabilitySearchReturn();
                            csr.setCapabilityIssueDate(issueDateResult);
                            csr.setCapabilityID(capabilityIDResult);

                            result.add(csr);
                        }
                    }
                }
            } catch (XPathExpressionException ex) {
                LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
        return result;
    }*/

    /*public static void main(String[] args) {

        String resourceID = "file:/D:/Access/Capability_Keystore/cert_req.txt";
        File capabilitiesDir = new File("D:/Capabilities");
        String subject = "alice@acme.com";
        String operation = "Read";

        CapabilityXPathSearch cs = new CapabilityXPathSearch(capabilitiesDir);
        
        ArrayList<CapabilitySearchReturn> capabilities = cs.doSearchCapability(subject, resourceID, operation);

        Collections.sort(capabilities, new Comparator<CapabilitySearchReturn>() {
            public int compare(CapabilitySearchReturn o1, CapabilitySearchReturn o2) {
                XMLGregorianCalendar issueDate1 = o1.getCapabilityIssueDateToXmlGregorianCalendar();
                XMLGregorianCalendar isssueDate2 = o2.getCapabilityIssueDateToXmlGregorianCalendar();
                return issueDate1.compare(isssueDate2);
            }
        });
        LOGGER.log(Level.INFO, "\nRetrieved Capabilities - Sort By Date \n");
        for (CapabilitySearchReturn capabilitySearchReturn : capabilities) {
            LOGGER.log(Level.INFO, "Capability issueDatde: {0}", capabilitySearchReturn.getCapabilityIssueDate());
        }
    }*/
}
